package com.zzz.codegen.entity;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.meta.Table;
import com.zzz.codegen.register.PropertyTypeRegister;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Accessors(chain = true)
@Getter
@Setter
public class TableVO implements Serializable {
    private String tableName;

    private String entityName;
    /**
     * 注释
     */
    private String comment;
    /**
     * 主键字段名列表
     */
    private Set<String> pkNames = new LinkedHashSet<>();

    private List<ColumnVO> columns = new ArrayList<>();

    public static TableVO cp(Table table) {
        TableVO t = new TableVO();
        t.setTableName(table.getTableName());
        t.setComment(table.getComment());
        t.setPkNames(table.getPkNames());
        table.getColumns().forEach(column -> {
            ColumnVO c = new ColumnVO();
            c.setTableName(column.getTableName());
            c.setComment(column.getComment());
            c.setPk(column.isPk());
            c.setName(column.getName());
            c.setProperty(StrUtil.lowerFirst(StrUtil.toUnderlineCase(column.getName())));
            c.setPropertyTypeClz(PropertyTypeRegister.getPropertyTypeClz(column.getTypeEnum()));

            t.getColumns().add(c);
        });
        return t;
    }
}
